package com.example.prison.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.prison.config.Response;
import com.example.prison.entity.PushManagement;
import com.example.prison.service.PushManagementService;
import com.example.prison.service.SystemLogService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api("推送管理")
@RestController
@RequestMapping("/api")
public class PushManagementController {

    @Autowired
    private PushManagementService pushManagementService;

    @Autowired
    private SystemLogService systemLogService;

    @ApiOperation("获取推送列表")
    @GetMapping("/getPush")
    public Response<PageInfo<PushManagement>> getPush(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        log.info("获取推送列表");
        PageInfo<PushManagement> pageInfo = pushManagementService.get(pageNumber, pageSize);
        return Response.success("获取成功！", pageInfo);
    }

    @ApiOperation("新增推送")
    @PostMapping("/addPush")
    public Response<?> addPush(@RequestBody PushManagement push) {
        log.info("新增推送");
        systemLogService.add("新增推送");
        int result = pushManagementService.add(push);
        if (result == -1) {
            return Response.error("推送已存在！");
        }
        return Response.success("添加成功！");
    }

    @ApiOperation("删除推送")
    @PostMapping("/deletePush")
    public Response<?> deletePush(@Parameter(description = "推送ID") @RequestBody Integer[] ids) {
        log.info("删除推送");
        systemLogService.add("删除推送");
        pushManagementService.delete(ids);
        return Response.success("删除成功！");

    }

    @ApiOperation("编辑推送")
    @PostMapping("/updatePush")
    public Response<?> updatePush(@RequestBody PushManagement push) {
        log.info("编辑推送");
        systemLogService.add("编辑推送");
        Integer id = push.getId();
        int result = pushManagementService.update(push, id);
        if (result == -1) {
            return Response.error("更新失败！该等级警报推送已存在！");
        } else {
            return Response.success("更新成功！");
        }
    }

    @ApiOperation("依据id获取推送列表")
    @GetMapping("/getPushById")
    public Response<PushManagement> getPushById(@Parameter(description = "推送ID")@RequestParam(value = "pushId") Integer pushId) {
        log.info("依据id获取推送列表");
        systemLogService.add("依据id获取推送列表");
        PushManagement push = pushManagementService.getById(pushId);
        if(ObjectUtils.isEmpty(push)) {
            return Response.error("此推送id不存在！");
        }
        return Response.success("获取成功！", push);
    }

    @ApiOperation("依据报警等级获取推送列表")
    @GetMapping("/getPushByAlarmLevel")
    public Response<PageInfo<PushManagement>> getPushByAlarmLevel(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                                  @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                                  @Parameter(description = "报警等级")@RequestParam(value = "alarmLevel") String alarmLevel) {
        log.info("依据报警等级获取推送列表");
        PageInfo<PushManagement> pageInfo = pushManagementService.getPushByAlarmLevel(pageNumber, pageSize,alarmLevel);
        return Response.success("获取成功！", pageInfo);
    }

//    @ApiOperation("推送处理")
//    @PostMapping("/handlePush")
//    public Response<?> handlePush(@RequestBody PushManagement push) {
//        log.info("推送处理");
//        systemLogService.add("推送处理");
//        Integer id = push.getId();
//        int result = pushManagementService.handlePush(push, id);
//            return Response.success("推送处理成功！");
//    }

}
